/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.PlannedMonthlyDemand;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.PlannedMonthlyDemandBeanLocal;
import session.stateless.MRP.SaleForecast.SaleForecastBeanLocal;

/**
 *
 * @author zhozho
 */
@ManagedBean(name="plannedMonthlyDemand")
@ViewScoped
public class PlannedMonthlyDemandManagerBean {
    @EJB
    PlannedMonthlyDemandBeanLocal plannedMonthlyDemandLocal;
    @EJB 
    private SaleForecastBeanLocal saleForecastBeanLocal;
    
    private String startMonth;
    private String endMonth;
    private List<ColumnModel> simpleColumns;
    
    private List<DataObject> dataList;

    public List<DataObject> getDataList() {
        return dataList;
    }

    public void setDataList(List<DataObject> dataList) {
        this.dataList = dataList;
    }
    
    public PlannedMonthlyDemandManagerBean() {
        
    }

    public List<ColumnModel> getSimpleColumns() {
        return simpleColumns;
    }

    public void setSimpleColumns(List<ColumnModel> simpleColumns) {
        this.simpleColumns = simpleColumns;
    }

    public String getEndMonth() {
        return endMonth;
    }

    public void setEndMonth(String endMonth) {
        this.endMonth = endMonth;
    }

    public String getStartMonth() {
        return startMonth;
    }

    public void setStartMonth(String startMonth) {
        this.startMonth = startMonth;
    }
    
    
    public void initView(PhaseEvent event){
        if(startMonth == null) 
        {
            startMonth = (String)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("startMonth");            
        }
        if(endMonth == null) 
        {
            endMonth = (String)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("endMonth");            
        }
        createDynamicColumns();
    }
    
    
    private void createDynamicColumns() {  
        dataList = new ArrayList<DataObject>();
        dataList.add(gatherSalesForecastInfo());
        dataList.add(gatherProductionPlan());
        dataList.add(gatherInventory());
        dataList.add(gatherWorkingDays());
        dataList.add(gatherCapacity());
        dataList.add(gatherUtilization());
        
        simpleColumns = new ArrayList<ColumnModel>(); 
        simpleColumns.add(new ColumnModel("Sales and Operation Planning", "type"));
        //
        int startIndex = GenerateSalesOperationPlan.monthList.indexOf(startMonth);
        int endIndex = GenerateSalesOperationPlan.monthList.indexOf(endMonth);
        for (int i = startIndex; i <= endIndex; ++i) {
            String attrib = GenerateSalesOperationPlan.monthList.get(i).substring(0, 3).toLowerCase();            
            simpleColumns.add(new ColumnModel(GenerateSalesOperationPlan.monthList.get(i), attrib));
        }
    }  
    
    private DataObject gatherSalesForecastInfo() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp = GenerateSalesOperationPlan.monthList.get(i);
            int year = Integer.parseInt(temp.substring(temp.length() - 4));
            temp = temp.substring(0, temp.length() - 5);// January - December
            //monthIndex = map from month name to ( Jan -> 0, ..., Dec -> 11)
            int monthIndex = findMonthIndex(temp);
            list[monthIndex] = plannedMonthlyDemandLocal.findByMonth(GenerateSalesOperationPlan.monthList.get(i)).getMonthlyForecast();
            
            //list[monthIndex] = Integer.toString(saleForecastBeanLocal.getMonthlySaleForecasts(year, monthIndex, monthIndex).get(0).getSaleForecastMonth());
            if (list[monthIndex] == null)
            {
                list[monthIndex] = "-";
            }
        }
        DataObject result = new DataObject("Sales Forecast",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherProductionPlan() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp = GenerateSalesOperationPlan.monthList.get(i);
            temp = temp.substring(0, temp.length() - 5);// January - December
            list[findMonthIndex(temp)] = plannedMonthlyDemandLocal.findByMonth(GenerateSalesOperationPlan.monthList.get(i)).getProductionPlan().toString();
            if (list[findMonthIndex(temp)] == null)
            {
                list[findMonthIndex(temp)] = "-";
            }
        }
        DataObject result = new DataObject("Production Plan",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherInventory() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp = GenerateSalesOperationPlan.monthList.get(i);
            temp = temp.substring(0, temp.length() - 5);// January - December
            list[findMonthIndex(temp)] = plannedMonthlyDemandLocal.findByMonth(GenerateSalesOperationPlan.monthList.get(i)).getInventory().toString();
            if (list[findMonthIndex(temp)] == null)
            {
                list[findMonthIndex(temp)] = "-";
            }
        }
        DataObject result = new DataObject("Inventory",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private int findMonthIndex(String monthName) {
        for (int i = 0; i < 12; ++i) {
            if (GenerateSalesOperationPlan.monthName[i].compareTo(monthName) == 0)
                return i;
        }
        return -1;
    }
    
    private DataObject gatherWorkingDays() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp = GenerateSalesOperationPlan.monthList.get(i);
            temp = temp.substring(0, temp.length() - 5);// January - December
            list[findMonthIndex(temp)] = plannedMonthlyDemandLocal.findByMonth(GenerateSalesOperationPlan.monthList.get(i)).getNumberOfWorkingDays().toString();
            if (list[findMonthIndex(temp)] == null)
            {
                list[findMonthIndex(temp)] = "-";
            }
        }
        DataObject result = new DataObject("Working Days",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherCapacity() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp = GenerateSalesOperationPlan.monthList.get(i);
            temp = temp.substring(0, temp.length() - 5);// January - December
            list[findMonthIndex(temp)] = plannedMonthlyDemandLocal.findByMonth(GenerateSalesOperationPlan.monthList.get(i)).getNutriCapacity().toString()
                                      + "/" + plannedMonthlyDemandLocal.findByMonth(GenerateSalesOperationPlan.monthList.get(i)).getPremiumCapacity().toString();
            if (list[findMonthIndex(temp)] == null)
            {
                list[findMonthIndex(temp)] = "-";
            }
        }
        DataObject result = new DataObject("Capacity(Nutri/Premium)",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherUtilization() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp = GenerateSalesOperationPlan.monthList.get(i);
            temp = temp.substring(0, temp.length() - 5);// January - December
            list[findMonthIndex(temp)] = plannedMonthlyDemandLocal.findByMonth(GenerateSalesOperationPlan.monthList.get(i)).getUtilization().toString() + "%";
            if (list[findMonthIndex(temp)] == null)
            {
                list[findMonthIndex(temp)] = "-";
            }
        }
        DataObject result = new DataObject("Utilization",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    static public class ColumnModel implements Serializable {  
  
        private String header;  
        private String property;
  
        public ColumnModel(String header, String property) {  
            this.header = header;  
            this.property = property;  
        }  
  
        public String getHeader() {  
            return header;  
        } 
        
        public String getProperty(){
            return property;
        }
    }  
    
    static public class DataObject implements Serializable{
        private String type;
        private String jan;
        private String feb;
        private String mar;
        private String apr;
        private String may;
        private String jun;
        private String jul;
        private String aug;
        private String sep;
        private String oct;
        private String nov;
        private String dec;

        public DataObject(String type, String jan, String feb, String mar, String apr, String may, String jun, String jul, String aug, String sep, String oct, String nov, String dec) {
            this.type = type;
            this.jan = jan;
            this.feb = feb;
            this.mar = mar;
            this.apr = apr;
            this.may = may;
            this.jun = jun;
            this.jul = jul;
            this.aug = aug;
            this.sep = sep;
            this.oct = oct;
            this.nov = nov;
            this.dec = dec;
        }

        public String getApr() {
            return apr;
        }

        public String getAug() {
            return aug;
        }

        public String getDec() {
            return dec;
        }

        public String getFeb() {
            return feb;
        }

        public String getJan() {
            return jan;
        }

        public String getJul() {
            return jul;
        }

        public String getJun() {
            return jun;
        }

        public String getMar() {
            return mar;
        }

        public String getMay() {
            return may;
        }

        public String getNov() {
            return nov;
        }

        public String getOct() {
            return oct;
        }

        public String getSep() {
            return sep;
        }

        public String getType() {
            return type;
        }
    }
}
